package com.tgy.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicIntegerArray;

/**
 * @Author: tgy
 * @Date: 12/8/20 3:44 PM
 */
public class AtomicArray {

    public static void main(String[] args) throws InterruptedException {

       final int len = 5;
        int[] num1 = new int[len];
        AtomicIntegerArray atomicIntegerArray = new AtomicIntegerArray(num1);

        CountDownLatch countDownLatch = new CountDownLatch(10);


        for (int i = 0; i < 10; i++) {

            new Thread(()->{

                for (int j = 0; j < 10000; j++) {

                    atomicIntegerArray.incrementAndGet(j % len);
                }

                countDownLatch.countDown();
            },"t" + i).start();
        }

        countDownLatch.await();


        for (int i = 0; i < len; i++) {

            System.out.print(atomicIntegerArray.get(i) + ",");
        }


    }
}
